package org.bee.model.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.bee.PageUtil;
import org.bee.enums.ResultCode;
import org.bee.model.BaseModel;

import java.util.Date;

/**
 * 通用返回数据结构
 */
public class Result implements BaseModel {
    /**
     * 接口访问时间/调用时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date msgTime = new Date();
    private Boolean success;
    private String code;
    private String msg;
    private Object data;

    public Result() {
    }

    public Result(boolean success) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : ResultCode.COMMON_FAIL.getCode();
        this.msg = success ? ResultCode.SUCCESS.getDesc() : ResultCode.COMMON_FAIL.getDesc();
    }

    public Result(boolean success, ResultCode resultEnum) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : (resultEnum == null ? ResultCode.COMMON_FAIL.getCode() : resultEnum.getCode());
        this.msg = success ? ResultCode.SUCCESS.getDesc() : (resultEnum == null ? ResultCode.COMMON_FAIL.getDesc() : resultEnum.getDesc());
    }

    public Result(boolean success, Object data) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : ResultCode.COMMON_FAIL.getCode();
        this.msg = success ? ResultCode.SUCCESS.getDesc() : ResultCode.COMMON_FAIL.getDesc();
        this.data = data;
    }

    public Result(boolean success, ResultCode resultEnum, Object data) {
        this.success = success;
        this.code = success ? ResultCode.SUCCESS.getCode() : (resultEnum == null ? ResultCode.COMMON_FAIL.getCode() : resultEnum.getCode());
        this.msg = success ? ResultCode.SUCCESS.getDesc() : (resultEnum == null ? ResultCode.COMMON_FAIL.getDesc() : resultEnum.getDesc());
        this.data = data;
    }

    public Date getMsgTime() {
        return msgTime;
    }

    public void setMsgTime(Date msgTime) {
        this.msgTime = msgTime;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public void setPageData(Object data){
        Page page = PageUtil.getPage();
        page.setOthers(data);
        this.data = page;
    }
}
